# -*- coding:utf-8 -*-
# @Time : 2021/2/25 17:08
# @Author : Administrator
# @File : chinaz_audio.py
# @Software: PyCharm
# @Motto: good good study,day day up

import requests
from parsel import Selector
from loguru import logger
from pathlib import Path
import multiprocessing
import os



def get_response(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    # response.encoding = response.apparent_encoding
    return response


def save(name, audio_url):
    header = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    audio_content = requests.get(url=audio_url, headers=header).content
    audio_base = Path.cwd().parent / 'audio'
    audio_name = audio_base.joinpath(f'{name}.mp3')
    if not os.path.exists(audio_base):
        logger.info(f'{audio_base}目录不存在,正在创建....')
        os.mkdir(audio_base)
    logger.info(f'正在写入{name}音频文件....')
    with open(audio_name, 'wb') as f:
        f.write(audio_content)


def main(url):
    response = get_response(url)
    selector = Selector(response.text)
    items = selector.xpath('//div[@class="audio-item"]')
    for item in items:
        name = item.xpath('.//div[@class="right-head"]//p[@class="name"]/text()').get().strip()
        src = item.xpath('.//audio/@src').get()
        audio_url = 'https:' + src
        # print(name, audio_url)
        save(name, audio_url)


if __name__ == '__main__':
    for page in range(1,6):
        p = multiprocessing.Process(target=main,args=(f'https://sc.chinaz.com/yinxiao/index_{page}.html',))
        p.start()